package com.my.chapter15;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class practice35 {
    @SuppressWarnings("unchecked")
    static void oldStyleMethod(List c) {
        c.add(new Breve());
    }

    public static void main(String[] args) {
        List<Americano> am1 = new ArrayList<Americano>();
        oldStyleMethod(am1); // Quietly accepts a Breve
        List<Americano> am2 = Collections.checkedList(
                new ArrayList<Americano>(), Americano.class);
        try {
            oldStyleMethod(am2); // Throws an exception
        } catch(Exception e) {
            System.out.println(e);
        }
        List<Coffee> coffees = Collections.checkedList(
                new ArrayList<Coffee>(), Coffee.class);
        coffees.add(new Americano());
        coffees.add(new Breve());
    }
}

class Coffee{
    private static long counter = 0;
    private final long id = counter++;

    @Override
    public String toString() {
        return getClass().getSimpleName() + " " + id;
    }
}

class Americano extends Coffee {}

class Breve extends Coffee {}
